package cn.ytr.project.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import cn.ytr.project.domain.AdminUser;
import cn.ytr.project.mapper.AdminUserMapper;
import cn.ytr.project.service.IAdminUserService;
import cn.ytr.project.util.MybatisUtil;

public class AdminUserServiceImpl implements IAdminUserService {
	
	@Override
	public AdminUser selectByUsernameAndPassword(String username, String password) {
		SqlSession session = MybatisUtil.getSession();
		AdminUserMapper adminUserMapper = session.getMapper(AdminUserMapper.class);
		AdminUser adminUser = adminUserMapper.selectByUsernameAndPassword(username, password);
		session.close();
		return adminUser;
	}

	@Override
	public List<AdminUser> selectForList() {
		SqlSession session = MybatisUtil.getSession();
		AdminUserMapper adminUserMapper = session.getMapper(AdminUserMapper.class);
		List<AdminUser> list = adminUserMapper.selectForList();
		session.close();
		return list;
	}
	
	@Test
	public void selectForList1() {
		SqlSession session = MybatisUtil.getSession();
		AdminUserMapper adminUserMapper = session.getMapper(AdminUserMapper.class);
		List<AdminUser> list = adminUserMapper.selectForList();
		session.close();
		for (AdminUser adminUser : list) {
			System.out.println(adminUser);
		}
	}

	@Override
	public void insert(AdminUser adminUser) {
		SqlSession session = MybatisUtil.getSession();
		AdminUserMapper adminUserMapper = session.getMapper(AdminUserMapper.class);
		adminUserMapper.insert(adminUser);
		session.commit();
		session.close();
	}

	@Override
	public void update(AdminUser adminUser) {	
		SqlSession session = MybatisUtil.getSession();
		AdminUserMapper adminUserMapper = session.getMapper(AdminUserMapper.class);
		adminUserMapper.update(adminUser);
		session.commit();
		session.close();
	}

	@Override
	public AdminUser selectByUsername(String username) {
		SqlSession session = MybatisUtil.getSession();
		AdminUserMapper adminUserMapper = session.getMapper(AdminUserMapper.class);
		AdminUser adminUser = adminUserMapper.selectByUsername(username);
		session.close();
		return adminUser;
	}

	@Override
	public AdminUser selectById(Long id) {
		SqlSession session = MybatisUtil.getSession();
		AdminUserMapper adminUserMapper = session.getMapper(AdminUserMapper.class);
		AdminUser adminUser = adminUserMapper.selectById(id);
		session.close();
		return adminUser;
	}

}
